package com.ijiuchuang.jc_localization.model.tools;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.bean.entity.BleDevice;
import com.ijiuchuang.jc_localization.ble.BleFilter;
import com.ijiuchuang.jc_localization.ble.BleUtil;
import com.ijiuchuang.jc_localization.contract.tools.ToolsRssiContract;
import com.ijiuchuang.jc_localization.tools.utils.HexUtil;
import com.ijiuchuang.jc_localization.tools.utils.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Veev on 2016/6/18
 *
 * @link 384506557@qq.com
 */
public class ToolsRssiModel implements ToolsRssiContract.Model {

    @Override
    public int getBleEnableRes() {
        return R.drawable.ic_bluetooth_searching_white_24dp;
    }

    @Override
    public int getBleDisEnableRes() {
        return R.drawable.ic_bluetooth_disabled_white_24dp;
    }

    private List<BleDevice> bleDevices = new ArrayList();
    private HashMap<String, BleDevice> bleDeviceMap = new HashMap<>();
    private HashMap<String, Integer> bleDeviceNumber = new HashMap<>();

    @Override
    public void startScan(final BleDeviceChangedListener listener) {
        BluetoothLeScanner scanner = BleUtil.getBluetoothAdapter().getBluetoothLeScanner();
        scanner.startScan(new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                BluetoothDevice device = result.getDevice();
                int rssi = result.getRssi();
                byte scanRecord[] = result.getScanRecord().getBytes();
                if (bleDeviceMap.containsKey(device.getAddress())) {
                    // 更新
                    double preRssi = BleFilter.KalmanFiltering(bleDeviceMap.get(device.getAddress()).getPreRssi(), rssi);
                    bleDeviceMap.get(device.getAddress()).setPreRssi(preRssi);
                    bleDeviceMap.get(device.getAddress()).setRssi(rssi);
                    bleDeviceMap.get(device.getAddress()).setBcData(HexUtil.encodeHexStr(scanRecord));

                    listener.onUpdated(bleDeviceNumber.get(device.getAddress()).intValue(), bleDeviceMap.get(device.getAddress()));
                } else if (bleDeviceMap.isEmpty()){
                    // 创建
                    BleDevice bleDevice = new BleDevice(device.getName(), device.getAddress(), rssi, rssi, HexUtil.encodeHexStr(scanRecord));
                    bleDeviceMap.put(device.getAddress(), bleDevice);
                    bleDeviceNumber.put(device.getAddress(), bleDeviceNumber.size());
                    bleDevices.add(bleDevice);

                    listener.onCreated(bleDevices);
                } else {
                    // 添加
                    BleDevice bleDevice = new BleDevice(device.getName(), device.getAddress(), rssi, rssi, HexUtil.encodeHexStr(scanRecord));
                    bleDeviceMap.put(device.getAddress(), bleDevice);
                    bleDeviceNumber.put(device.getAddress(), bleDeviceNumber.size());

                    listener.onAdd(bleDevice);
                }
            }
        });
        /*BleUtil.getBluetoothAdapter().startLeScan(new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
                if (bleDeviceMap.containsKey(device.getAddress())) {
                    // 更新
                    double preRssi = BleFilter.KalmanFiltering(bleDeviceMap.get(device.getAddress()).getPreRssi(), rssi);
                    bleDeviceMap.get(device.getAddress()).setPreRssi(preRssi);
                    bleDeviceMap.get(device.getAddress()).setRssi(rssi);
                    bleDeviceMap.get(device.getAddress()).setBcData(HexUtil.encodeHexStr(scanRecord));

                    listener.onUpdated(bleDeviceNumber.get(device.getAddress()).intValue(), bleDeviceMap.get(device.getAddress()));
                } else if (bleDeviceMap.isEmpty()){
                    // 创建
                    BleDevice bleDevice = new BleDevice(device.getName(), device.getAddress(), rssi, rssi, HexUtil.encodeHexStr(scanRecord));
                    bleDeviceMap.put(device.getAddress(), bleDevice);
                    bleDeviceNumber.put(device.getAddress(), bleDeviceNumber.size());
                    bleDevices.add(bleDevice);

                    listener.onCreated(bleDevices);
                } else {
                    // 添加
                    BleDevice bleDevice = new BleDevice(device.getName(), device.getAddress(), rssi, rssi, HexUtil.encodeHexStr(scanRecord));
                    bleDeviceMap.put(device.getAddress(), bleDevice);
                    bleDeviceNumber.put(device.getAddress(), bleDeviceNumber.size());

                    listener.onAdd(bleDevice);
                }
            }
        });*/
    }

    @Override
    public void stopScan() {

    }
}
